<?php

class Adminindex extends Controller {

	public $userLog;
	public $users;
	public $pag;
	public $pags;
	public $totalPage;
	public $curPage;
	public $pageMin;

	public function __construct() {
		parent::__construct();
		$this->_initalize();
	}

	public function __destruct() {
		parent::__destruct();
	}

	public function index() {
		$this->library('pagination');
		$uri = str_replace("/les4_2/", '', $_SERVER['REQUEST_URI']);
		//echo $uri;
		$this->curPage = isset($this->_get['page'])?intval($this->_get['page']):1;
		$this->pageMin = isset($this->_get['pageMin'])?intval($this->_get['pageMin']):1;

		//$curPage = isset($this->_get['page'])?intval($this->_get['page']):1;
		//$pageMin = isset($this->_get['pageMin'])?intval($this->_get['pageMin']):1;

		$numRows = count($this->user->gets());
		$this->pagination->KhoiTaoTrang($this->curPage,8,$this->pageMin,$numRows);
		//$this->pag = $this->pagination->LayDsTrang("index.php?c=adminIndex&");
		//$this->pags = $this->pagination->gets($this->pag);
		$this->users = $this->user->getList(NULL, ($this->curPage -1) * 8);

		if(isset($this->_get['search'])){
			$search = $this->_get['search'];
			unset($search['from']);
			unset($search['to']);
			//var_dump($numRows);
			$numRows = count($this->user->gets($search));
			$this->pagination->KhoiTaoTrang($this->curPage, 8, $this->pageMin, $numRows);
			//$this->pag = $this->pagination->LayDsTrang($uri . "&");

			/*if((bool)$this->curPage){
				$this->pag = $this->pagination->LayDsTrang("index.php?c=adminIndex&page=" . $this->curPage);
			}
			if((bool)$this->pageMin){
				$this->pag = $this->pagination->LayDsTrang("index.php?c=adminIndex&page=" . $this->curPage . "&pageMin=" . $this->pageMin);
			}
			*/
			//$this->pags = $this->pagination->gets($this->pag);
			$this->users = $this->user->getList($search, ($this->curPage -1) * 8);
		}

		$this->pag = $this->pagination->LayDsTrang($uri . "&");
		$this->pags = $this->pagination->gets($this->pag);
		$this->totalPage = round($numRows / 8, 0);
		$this->template('admin/index');
	}

	private function _initalize() {
		if (isset($this->_session['user'])) {
			$this->userLog = $this->_session['user']['info'];
			if ($this->userLog->level != 1) {
				header('Location:index.php?c=login');
			}
		} else {
			header('Location:index.php?c=login');
		}

		$this->model('user');
		$this->user->id = $this->userLog->id;
	}

	public function deleteuser(){
		if(isset($this->_post['user'])){
			$id = $this->_post['user']['id'];
			$result = $this->user->delete($id);
			if((bool)$result){
				echo "{result:1, id:{$id}}";
			}
			else {
				echo "{result:0, id:{$id}}";
			}
		}
	}

	public function deleteusers(){
		if(isset($this->_post['user'])){
			$ids = json_decode($this->_post['user']['ids']);
			foreach ($ids as $id){
				$result = $this->user->delete($id);
			}

			if((bool)$result){
				echo "{result:1, id:{$id}}";
			}
			else {
				echo "{result:0, id:{$id}}";
			}
		}
	}
}
?>
